/**
 * 
 */
package com.rangers.auth.sample.jpa.ctrl;

import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.rangers.auth.base.details.AuthUser;
import com.rangers.auth.base.service.UserService;
import com.rangers.auth.sample.jpa.entity.User;
import com.rangers.auth.sample.jpa.repo.UserRepo;

/**
 * 账户管理ctrl
 * @version 1.0
 * @author fuhw
 * @date 2017年8月21日 上午9:55:25
 */
@RestController
@RequestMapping("/users")
public class UserCtrl {
	
	private final UserService<User, AuthUser, UserRepo, String> userService;

	@Autowired
	public UserCtrl(UserService<User, AuthUser, UserRepo, String>  userService) {
		super();
		this.userService = userService;
	}
	
	
	@GetMapping("/{id}")
	public User get(@PathVariable("id") String id) {
		return userService.get(id);
	}
	
	@GetMapping
	public List<User> list() {
		return userService.list();
	}

	@PostMapping
	public User post(@Valid @RequestBody User t) {
		return userService.create(t);
	}

	@PutMapping
	public User put(@Valid @RequestBody User t) {
		return userService.update(t);
	}

	@DeleteMapping("/{id}")
	public void delete(@PathVariable("id") String id) {
		userService.delete(id);
	}

	@GetMapping("/find-by-username")
	public User findByUsername(@RequestParam("username") String username) {
		return userService.findByUsername(username);
	}

}
